//
//  ByMineSettingVC.m
//  hutouben_ios
//
//  Created by justiOS on 16/11/29.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByMineSettingVC.h"

// vc
#import "ByAboutUsVC.h"             // 关于我们
@interface ByMineSettingVC ()<UITableViewDataSource, UITableViewDelegate>
// view
@property (nonatomic, strong) UITableView       *table;
@property (nonatomic, strong) UISwitch          *pushSwitch;            // 推送开关
@property (nonatomic, strong) UIButton          *logoutButton;          // 登陆按钮

// data
@property (nonatomic, strong) NSArray           *subjects;
@property (nonatomic, strong) NSMutableArray    *subjectDetails;
@end

@implementation ByMineSettingVC
#pragma mark - getter
- (UISwitch *)pushSwitch {
    if (!_pushSwitch) {
        _pushSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 25)];
//        BOOL ispush = [ByCommonCache getPushInfo];
//        _pushSwitch.on = !ispush;
        [_pushSwitch addTarget:self action:@selector(pushSwitchClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _pushSwitch;
}
- (UIButton *)logoutButton {
    if (!_logoutButton) {
        _logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _logoutButton.frame = CGRectMake(20, 0, SCREEN_WIDTH - 40, 40);
        _logoutButton.backgroundColor = HTB_THEME_COLOR;
        [_logoutButton setTitle:@"退出登录" forState:UIControlStateNormal];
        [_logoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_logoutButton addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    }
    return _logoutButton;
}
- (NSArray *)subjects {
    if (!_subjects) {
        _subjects = @[@"消息推送开关", @"当前版本", @"关于虎头奔", @""];
    }
    return _subjects;
}
- (NSMutableArray *)subjectDetails {
    if (!_subjectDetails) {
        NSArray *temp = @[@"", [NSString stringWithFormat:@"v%@",APP_VERSION], @"", @""];
        _subjectDetails = [[NSMutableArray alloc] initWithArray:temp];
    }
    return _subjectDetails;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    
    [self addLeftBackBarButtonItem];
    
    [self createTableView];
    [self createLogoutButton];
}


#pragma mark - create view
// 创建登出按钮
- (void)createLogoutButton {
    
    [self.view addSubview:self.logoutButton];
    self.logoutButton.top = _table.bottom + 20;
}
// 创建表视图
- (void)createTableView {
    
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150) style:UITableViewStyleGrouped];
    _table.scrollEnabled = NO;
    _table.backgroundColor = HTB_BG_WHITE;
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    self.view.backgroundColor = _table.backgroundColor;
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.subjects.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.font = FONT(14);
        cell.detailTextLabel.textColor = [UIColor lightGrayColor];
        cell.detailTextLabel.font = FONT(12);
    }
    
    cell.textLabel.text = self.subjects[indexPath.section];
    cell.detailTextLabel.text = self.subjectDetails[indexPath.section];
    
    if (indexPath.section == 0) {       // 推送开关
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryView = self.pushSwitch;
    }
    if (indexPath.section == 1) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 44;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 5;
    } else {
        return 2.5;
    }
    
}
// 设置组尾高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 2.5;
}

// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    // 关于我们
    if (indexPath.section == 2) {
//        ByWebViewController *vc = [[ByWebViewController alloc] initWithUrlString:@"about"];
//        vc.title = @"关于我们";
//        [self by_normalPush:vc hasNavi:YES];
        ByAboutUsVC *vc = [[ByAboutUsVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }
}






#pragma mark - 点击事件
- (void)pushSwitchClick:(UISwitch *)s {
    NSLog(@"%i", s.on);
//    [ByCommonCache setPushPrivateLetter:[NSString stringWithFormat:@"%i",!s.on]];
//    [[ZKIMKitHandle sharedInstance] pushMessageOnAndOff];
}
- (void)logout {
    [ByCommonCache setUserInfo:nil];
    [ByCoreToolAlert showOnlyText:@"成功登出"];
    [UMessage removeAlias:BY_USER.uid type:@"zhujia" response:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
        NSLog(@"解除 %@=---------%@",responseObject,error);
    }];
    [self dismissViewControllerAnimated:YES completion:^{
//        if(self.refreshBlock){
//            (self.refreshBlock());
//        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
